﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
#if !WINDOWS_PHONE


#endif

namespace DARE
{
    public class CNodeModel
    {

    #region field

        private Vector3 m_size;
        private Vector3 m_position;
        private VertexPositionColor[] m_vertices;
        private VertexBuffer m_vertexBuffer;
        private BasicEffect m_basicEffect;
        private bool m_show = false;

    #endregion

    #region properties

        public bool Show
        {
            get { return m_show; }
            set { m_show = value; }
        }

    #endregion

    #region ctor

        public CNodeModel()
        {
            Initialize();
        }

        private void Initialize()
        {
            m_size = Vector3.One;
            m_position = Vector3.Zero;
            if (CDare.Instance != null)
                m_basicEffect = CDare.Instance.XnaBasicEffect;
            SetUpVertices();
        }

    #endregion

    #region methods

        public void Highlight(bool light)
        {
            for (int i = 0; i < m_vertices.Length; ++i)
                if ((m_vertices[i].Color != Color.Blue) && 
                    (m_vertices[i].Color != Color.Red) && 
                    (m_vertices[i].Color != Color.Green))
                {
                    m_vertices[i].Color = light ? Color.White : Color.Gray;
                    m_vertexBuffer = new VertexBuffer(CDare.Instance.GraphicsDevice, typeof(VertexPositionColor), m_vertices.Length, BufferUsage.WriteOnly);
                    m_vertexBuffer.SetData(m_vertices);
                }
        }

        private void SetUpVertices()
        {
            m_vertices = new VertexPositionColor[12];
            int count = 0;

            // x axis
            m_vertices[count++] = new VertexPositionColor(new Vector3(0.0f, 0.0f, 0.0f), Color.Gray);
            m_vertices[count++] = new VertexPositionColor(new Vector3(0.4f, 0.0f, 0.0f), Color.Gray);
            m_vertices[count++] = new VertexPositionColor(new Vector3(0.4f, 0.0f, 0.0f), Color.Green);
            m_vertices[count++] = new VertexPositionColor(new Vector3(1.0f, 0.0f, 0.0f), Color.Green);

            // y axis
            m_vertices[count++] = new VertexPositionColor(new Vector3(0.0f, 0.0f, 0.0f), Color.Gray);
            m_vertices[count++] = new VertexPositionColor(new Vector3(0.0f, 0.4f, 0.0f), Color.Gray);
            m_vertices[count++] = new VertexPositionColor(new Vector3(0.0f, 0.4f, 0.0f), Color.Red);
            m_vertices[count++] = new VertexPositionColor(new Vector3(0.0f, 1.0f, 0.0f), Color.Red);

            // z axis
            m_vertices[count++] = new VertexPositionColor(new Vector3(0.0f, 0.0f, 0.0f), Color.Gray);
            m_vertices[count++] = new VertexPositionColor(new Vector3(0.0f, 0.0f, 0.4f), Color.Gray);
            m_vertices[count++] = new VertexPositionColor(new Vector3(0.0f, 0.0f, 0.4f), Color.Blue);
            m_vertices[count++] = new VertexPositionColor(new Vector3(0.0f, 0.0f, 1.0f), Color.Blue);

            // set the vertex buffer
            m_vertexBuffer = new VertexBuffer(CDare.Instance.GraphicsDevice, typeof(VertexPositionColor), m_vertices.Length, BufferUsage.WriteOnly);
            m_vertexBuffer.SetData(m_vertices);
        }

        public void Draw(Matrix world, Matrix view, Matrix projection)
        {
            if (!m_show)
                return;

            CDare.Instance.GraphicsDevice.SetVertexBuffer(m_vertexBuffer);

            m_basicEffect.VertexColorEnabled = true;
            m_basicEffect.LightingEnabled = false;

            m_basicEffect.World = world;
            m_basicEffect.View = view;
            m_basicEffect.Projection = projection;

            foreach (EffectPass pass in m_basicEffect.CurrentTechnique.Passes)
            {
                pass.Apply();
                CDare.Instance.GraphicsDevice.DrawPrimitives(PrimitiveType.LineList, 0, m_vertices.Length / 2);
            }
        }

    #endregion

    }
}
